package com.aaa.cs.service.impl;

import com.aaa.common.constants.ExceptionConstant;
import com.aaa.common.constants.ReturnCode;
import com.aaa.common.util.CustomException;
import com.aaa.common.util.Result;
import com.aaa.cs.dao.CarTypeDao;
import com.aaa.cs.entity.CarType;
import com.aaa.cs.service.CarTypeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * (CarType)表服务实现类
 *
 * @author makejava
 * @since 2020-11-18 15:47:13
 */
@Service("carTypeService")
public class CarTypeServiceImpl implements CarTypeService {
    @Resource
    private CarTypeDao carTypeDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Result queryById(Integer id) {
        return new Result(ReturnCode.SUCCESSS.getCode(),
                ReturnCode.SUCCESSS.getMsg(),this.carTypeDao.queryById(id));
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<CarType> queryAllByLimit(int offset, int limit) {
        return this.carTypeDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param carType 实例对象
     * @return 实例对象
     */
    @Override
    public CarType insert(CarType carType) {
        this.carTypeDao.insert(carType);
        return carType;
    }

    /**
     * 修改数据
     *
     * @param carType 实例对象
     * @return 实例对象
     */
    @Override
    public CarType update(CarType carType) {
        this.carTypeDao.update(carType);
        return (CarType) this.queryById(carType.getId()).getData();
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public Result deleteById(Integer id) {
        return new Result(ReturnCode.SUCCESSS.getCode(),
                ReturnCode.SUCCESSS.getMsg(),this.carTypeDao.deleteById(id));
    }

    @Override
    public Result queryAll() {
        return new Result(ReturnCode.SUCCESSS.getCode(),
                ReturnCode.SUCCESSS.getMsg(),this.carTypeDao.queryAll(new CarType()));
    }

    @Override
    public Result queryPageByParam(Integer pageNo, Integer pageSize, CarType carType) {
        if(pageNo==null||pageSize==null){
            throw new CustomException(ExceptionConstant.INVALID_ARGUMENT.getErrorCode(),
                    ExceptionConstant.INVALID_ARGUMENT.getErrorMessage());
        }
        //分页
        PageHelper.startPage(pageNo,pageSize);
        PageInfo<CarType> pageInfo = new PageInfo<CarType>(carTypeDao.queryAll(carType));
        return new Result(ReturnCode.SUCCESSS.getCode(),
                ReturnCode.SUCCESSS.getMsg(),pageInfo);
    }

    /**
     * 新增或修改数据
     *
     * @param carType 实例对象
     * @return 实例对象
     */
    @Override
    public Result insertOrUpdate(CarType carType) {
        int i = 0;
        if (carType.getId() != null && carType.getId() !=0) {
            i = carTypeDao.update(carType);
            if (i>0){
                return new Result(ReturnCode.SUCCESSS.getCode(),
                        ReturnCode.SUCCESSS.getMsg(),"修改成功");
            }else {
                return new Result(ReturnCode.FAIL.getCode(),
                        ReturnCode.FAIL.getMsg(),"修改失败");
            }
        }else {
            i = carTypeDao.insert(carType);
            if (i >0){
                return new Result(ReturnCode.SUCCESSS.getCode(),
                        ReturnCode.SUCCESSS.getMsg(),"添加成功");
            }else {
                return new Result(ReturnCode.FAIL.getCode(),
                        ReturnCode.FAIL.getMsg(),"添加失败");
            }
        }
    }
}